package com.twu.biblioteca;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

public class Users {

    private HashMap<String,UserDetails> users = new HashMap<String,UserDetails>();

    public Users()
    {
        users.put("111-1111", new UserDetails("Vaibhav", "vaibhavkhunger@gmail.com", "9871913619", makeMd5("Password@123")));
        users.put("111-1112", new UserDetails("Piyush", "piyush@gmail.com", "9041291916", makeMd5("Password@123")));
        users.put("111-1113", new UserDetails("Sidhant", "sidhant@gmail.com", "9877689008", makeMd5("Password@123")));
        users.put("111-1114", new UserDetails("Varun", "varun@gmail.com", "9878980891", makeMd5("Password@123")));
        users.put("111-1115", new UserDetails("Saransh", "saransh@gmail.com", "9897809211", makeMd5("Password@123")));
    }

    public HashMap<String, UserDetails> getUsers() {
        return users;
    }

    public void addUsers(String username, String name, String emailId, String phoneNo, String password)
    {
        users.put(username, new UserDetails(name,emailId,phoneNo,makeMd5(password)));
    }

    public Boolean authenticateUser(String username, String password) throws IllegalArgumentException
    {
        Map<String, UserDetails> map = users;
        for (Map.Entry<String, UserDetails> entry : map.entrySet()) {
            if(entry.getKey().equals(username))
            {
                if(entry.getValue().getPassword().equals(makeMd5(password)))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        return false;
    }

    public void displayUserDetails(String name)
    {
        Map<String, UserDetails> map = users;
        System.out.println("The user details are:\n");
        System.out.format("%-25s%-25s%s", "Name", "EmailId", "Phone No.\n");
        for (Map.Entry<String, UserDetails> entry : map.entrySet()) {
            if(entry.getKey().equalsIgnoreCase(name))
            {
                UserDetails details = entry.getValue();
                System.out.format("%-25s%-25s%s", details.getName(), details.getEmailId(), details.getPhoneNo()+"\n");
            }
        }
    }

    public String makeMd5(String toProcess)
    {
        try
        {
            byte[] objectBytes = toProcess.getBytes();
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.reset();
            md5.update(objectBytes);
            byte messageDigest[] = md5.digest();
            StringBuffer hexString = new StringBuffer();
            for (int i=0;i<messageDigest.length;i++)
            {
                hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
            }
            return hexString.toString();
        }
        catch(NoSuchAlgorithmException nsae)
        {
            return null;
        }
    }
}
